﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace MvcShop.Models.Repositories
{
    /// <summary>
    /// 查询条件类型
    /// </summary>
    public enum CriteriaType
    {
        /// <summary>
        /// 0 表示查询条件是个表达式
        /// </summary>
        Expression = 0,
        /// <summary>
        /// 1表示查询条件是个参数
        /// </summary>
        Parameter = 1
    }

    /// <summary>
    /// 查询条件参数类
    /// </summary>
    public class CriteriaParameter
    {
        /// <summary>
        /// 参数的值
        /// </summary>
        public object Value { get; private set; }
        /// <summary>
        /// 参数的名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 参数字段 ？如果你暂时不明白，在以后我们具体使用的时候一看就知道啦，别着急，慢慢来啊  
        /// </summary>
        public string Field { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="field"></param>
        public CriteriaParameter(string name, object value, string field)
        {
            this.Name = name;
            this.Value = value;
            this.Field = field;
        }
    }

    /// <summary>
    /// 查询条件接口
    /// </summary>
    public interface ICriteria
    {
        CriteriaType CriteriaType { get; }
        string Expression { get; }
        CriteriaParameter[] Parameters { get; }

        /// <summary>
        /// 并 操作 比如查询“姓名”+“金额” 等
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>

        ICriteria And(ICriteria other);
        ICriteria Or(ICriteria other);
        ICriteria Not();
        ICriteria BracketsAnd(ICriteria other);
        ICriteria BracketsOr(ICriteria other);
    }

    /// <summary>
    /// 排序类型
    /// </summary>
    public enum OrderType
    {
        ASC,
        DESC
    }

    /// <summary>
    /// 排序类
    /// </summary>
    public class Order
    {
        public OrderType OrderType { get; set; }
        /// <summary>
        /// 排序的字段
        /// </summary>
        public string[] Fields { get; set; }
        public Order(OrderType orderType, params string[] fields)
        {
            this.OrderType = orderType;
            this.Fields = fields;
        }

        public Order(params string[] fields) : this(OrderType.ASC, fields) { }

        /// <summary>
        /// 取出排序的表达式
        /// </summary>
        public string Expression
        {
            get
            {
                string str = string.Empty;
                foreach (var field in Fields)
                {
                    if (str.Length > 0)
                        str += ",";
                    str += string.Format("it.{0} {1}", field, OrderType.ToString());
                }
                return str;
            }
        }

        /// <summary>
        /// 排序 返回  IQueryable<T> 类型的结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public IQueryable<T> OrderFrom<T>(ObjectQuery<T> query) where T : class
        {
            switch (this.OrderType)
            {
                case OrderType.ASC:
                    return query.OrderBy(Expression);
                case OrderType.DESC:
                    return query.OrderBy(Expression);
            }
            return query;
        }

    }
}
